﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using ModelClassesLinqToSql.Domain.Repositories;
using ModelClassesLinqToSql.Domain.Models;

namespace ModelClassesLinqToSql.Domain
{
    public static class Cache
    {
        private enum QueryType
        {
            ProductionCompanies = 0,
            Employees = 1,
            JobTitles = 2
        }

        public static IEnumerable<ProductionCompany> GetProductionCompanies()
        {
            string ck = QueryType.ProductionCompanies.ToString();

            if (HttpContext.Current.Cache[ck] == null)
            {
                HttpContext.Current.Cache.Insert(ck,
                    new ProductionCompanyRepository().GetAll().ToList(),
                    null,
                    DateTime.MaxValue,
                    TimeSpan.FromMinutes(15));
            }

            return (IEnumerable<ProductionCompany>)HttpContext.Current.Cache[ck];
        }

        public static IEnumerable<Employee> GetEmployees()
        {
            string ck = QueryType.Employees.ToString();

            if (HttpContext.Current.Cache[ck] == null)
            {
                HttpContext.Current.Cache.Insert(ck,
                    new EmployeeRepository().GetAll().ToList(),
                    null,
                    DateTime.MaxValue,
                    TimeSpan.FromMinutes(15));
            }

            return (IEnumerable<Employee>)HttpContext.Current.Cache[ck];
        }

        public static IEnumerable<JobTitle> GetJobTitles()
        {
            string ck = QueryType.JobTitles.ToString();

            if (HttpContext.Current.Cache[ck] == null)
            {
                HttpContext.Current.Cache.Insert(ck,
                    new JobTitleRepository().GetAll().ToList(),
                    null,
                    DateTime.MaxValue,
                    TimeSpan.FromMinutes(15));
            }

            return (IEnumerable<JobTitle>)HttpContext.Current.Cache[ck];
        }
    }
}